package com.mzj.eagle.jobs.heartbeat.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.mzj.eagle.jobs.heartbeat.entity.Heartbeat;
import com.mzj.eagle.jobs.heartbeat.vo.ScaleMachineVo;

/**
 * 
* @ClassName: HeartbeatController 
* @Description: 心跳控制类
* @author JYQ
* @date 2018年1月4日 下午8:42:08 
*
 */
@RefreshScope
@RestController
@RequestMapping("/heartbeat")
public class HeartbeatController {

	private static final Logger LOG = LoggerFactory.getLogger(HeartbeatController.class);
	@Autowired
	private HeartbeatService heartbeatService;
	@Autowired
	SimpleMessageListenerContainer container;
	
	@RequestMapping(value="/stop",method = RequestMethod.GET)
    public String stop() {
    	try {
    		container.stop();
    		heartbeatService.heartbeatSave();
		} catch (Exception e) {
			LOG.error("stop error……");
			LOG.error(e.getMessage(),e);
			return "error";
		}
    	return "success";
    }
	
	@RequestMapping(value="/flush",method = RequestMethod.GET)
    public String flush() {
    	try {
    		heartbeatService.heartbeatSave();
		} catch (Exception e) {
			LOG.error("flush error……");
			LOG.error(e.getMessage(),e);
			return "error";
		}
    	return "success";
    }
	
	@RequestMapping(value="/start",method = RequestMethod.GET)
	public String start() {
		try {
			container.start();
		} catch (Exception e) {
			LOG.error("start error……");
			LOG.error(e.getMessage(),e);
			return "error";
		}
		return "success";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String getHeartbeat(@RequestParam String machineId) throws Exception {
		List<Heartbeat> list = heartbeatService.getHeartbeat(machineId);
		return JSON.toJSONString(list);
	}

	@RequestMapping(method = RequestMethod.POST)
	public Boolean saveHeartbeat(@RequestBody String heartbeat) throws Exception {
		return heartbeatService.insertData(heartbeat);
	}

	
}
